package definite.Guide.Book.Chap01_02;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class VerifierSample {

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			@Override
			public void run() {
				JFrame frame = new JFrame("Verifier Sample");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				JTextField textField1 = new JTextField();
				JTextField textField2 = new JTextField();
				JTextField textField3 = new JTextField();

				InputVerifier verifier = new InputVerifier() {

					@Override
					public boolean verify(JComponent input) {
						boolean returnValue;
						JTextField textField = (JTextField) input;
						try {
							Integer.parseInt(textField.getText());
							returnValue = true;
						} catch (NumberFormatException e) {
							System.out.println(e.getMessage());
							returnValue = false;
						}
						return false;
					}
				};

				textField1.setInputVerifier(verifier);
				textField3.setInputVerifier(verifier);

				frame.add(textField1, BorderLayout.NORTH);
				frame.add(textField2, BorderLayout.CENTER);
				frame.add(textField3, BorderLayout.SOUTH);
				frame.setSize(300, 200);
				frame.setResizable(false);
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(runner);
	}

}
